package com.ssm.study.controller;


import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.study.entity.User;
import com.ssm.study.service.IUserService;
/**
 * Created by bevin on 2017/4/29.
 */

@Controller
@RequestMapping("/")
public class LoginController {

    @Resource
    IUserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView LoginPost(
            @RequestParam(value = "username") String username,
            @RequestParam(value = "password") String password
    ){
        User user = null;
        //User user = userService.getUserByUsername(username);
        if (user == null){
            ModelAndView mav = new ModelAndView("/login_err.jsp");
            mav.addObject("Title", "登录错误");
            mav.addObject("Content", String.format("用户名 %s 不存在", username));
            return mav;
        }
        else if (!password.equals(user.getPassword())){
            ModelAndView mav = new ModelAndView("/login_err.jsp");
            mav.addObject("Title", "登录错误");
            mav.addObject("Content", "密码错误");
            return mav;
        }
        else {
            ModelAndView mav = new ModelAndView("/welcome.jsp");
            mav.addObject("Title", "Welcome" + user.getUsername());
            mav.addObject("Content", String.format("Hello %s", user.getUsername()));
            return mav;
        }
    }
}
